//
//  htvtablepage.m
//  PHApp
//
//  Created by zhangwq on 16/4/9.
//  Copyright © 2016年 HT. All rights reserved.
//

#import "htvtablepage.h"

@interface htvtablepage (){
    CGFloat width;
    CGFloat height;
    UIView *uv;
    CGFloat rowHeight;
    NSMutableArray *tablerray;//产品排名
    NSMutableArray *tablerray2;//客户排名
    NSMutableArray *tablerray3;//业务排名
    UIColor *backcolor;
    CGFloat maxQty;
    CGFloat maxQty2;
    CGFloat maxQty3;
}

@end

@implementation htvtablepage

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    width = size.width;
    height = size.height;
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    self.tableView.separatorStyle=NO;
    
    backcolor=[UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0];
    
    [self createT1];
    [self createT2];
    [self createT3];
    
    [self.tableView reloadData];
    
    if([self.formid isEqualToString:@"1"]){
        self.tableView.contentOffset=CGPointMake(0, 0);
    }
    if([self.formid isEqualToString:@"2"]){
        CGFloat cgheight=70+[tablerray count]*25;
        self.tableView.contentOffset=CGPointMake(0, cgheight);
    }
    if([self.formid isEqualToString:@"3"]){
        CGFloat cgheight=150+[tablerray count]*25+[tablerray2 count]*25;
        self.tableView.contentOffset=CGPointMake(0, cgheight);
    }
}


-(void)createT1{
    //产品排名
    tablerray=[NSMutableArray array];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"ML140928",@"name",
                          @"90", @"qty",
                          nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML120938",@"name",
                           @"60", @"qty",
                           nil];
    NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML150922",@"name",
                           @"56", @"qty",
                           nil];
    NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML130128",@"name",
                           @"23", @"qty",
                           nil];
    NSDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML113924",@"name",
                           @"64", @"qty",
                           nil];
    NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML130945",@"name",
                           @"75", @"qty",
                           nil];
    NSDictionary *dict7 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML130426",@"name",
                           @"34", @"qty",
                           nil];
    NSDictionary *dict8 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML160728",@"name",
                           @"57", @"qty",
                           nil];
    NSDictionary *dict9 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"ML142936",@"name",
                           @"23", @"qty",
                           nil];
    NSDictionary *dict10 = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"ML150423",@"name",
                            @"87", @"qty",
                            nil];
    [tablerray addObject:dict];
    [tablerray addObject:dict2];
    [tablerray addObject:dict3];
    [tablerray addObject:dict4];
    [tablerray addObject:dict5];
    [tablerray addObject:dict6];
    [tablerray addObject:dict7];
    [tablerray addObject:dict8];
    [tablerray addObject:dict9];
    [tablerray addObject:dict10];
    
    maxQty=0;
    for(NSInteger i=0;i<[tablerray count];i++){
        NSDictionary *dict=[tablerray objectAtIndex:i];
        CGFloat qty=[[dict objectForKey:@"qty"]floatValue];
        if(qty>maxQty){
            maxQty=qty;
        }
    }
}

-(void)createT2{
    //客户排名
    tablerray2=[NSMutableArray array];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"PRADA",@"name",
                          @"920", @"qty",
                          nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"GUESS",@"name",
                           @"603", @"qty",
                           nil];
    NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"armani",@"name",
                           @"56", @"qty",
                           nil];
    NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"calvinklein",@"name",
                           @"1123", @"qty",
                           nil];
    NSDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"路易·威登",@"name",
                           @"624", @"qty",
                           nil];
    NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"香奈尔",@"name",
                           @"75", @"qty",
                           nil];
    NSDictionary *dict7 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"范思哲",@"name",
                           @"344", @"qty",
                           nil];
    NSDictionary *dict8 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"迪奥",@"name",
                           @"537", @"qty",
                           nil];
    NSDictionary *dict9 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"gucci",@"name",
                           @"223", @"qty",
                           nil];
    NSDictionary *dict10 = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"瓦伦蒂诺",@"name",
                            @"187", @"qty",
                            nil];
    [tablerray2 addObject:dict];
    [tablerray2 addObject:dict2];
    [tablerray2 addObject:dict3];
    [tablerray2 addObject:dict4];
    [tablerray2 addObject:dict5];
    [tablerray2 addObject:dict6];
    [tablerray2 addObject:dict7];
    [tablerray2 addObject:dict8];
    [tablerray2 addObject:dict9];
    [tablerray2 addObject:dict10];
    
    maxQty2=0;
    for(NSInteger i=0;i<[tablerray2 count];i++){
        NSDictionary *dict=[tablerray2 objectAtIndex:i];
        CGFloat qty=[[dict objectForKey:@"qty"]floatValue];
        if(qty>maxQty2){
            maxQty2=qty;
        }
    }
}

-(void)createT3{
    //业务排名
    tablerray3=[NSMutableArray array];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"李灵黛",@"name",
                          @"20", @"qty",
                          nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"冷文卿",@"name",
                           @"30", @"qty",
                           nil];
    NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"阴露萍",@"name",
                           @"46", @"qty",
                           nil];
    NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"柳兰歌",@"name",
                           @"25", @"qty",
                           nil];
    NSDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"秦水支",@"name",
                           @"61", @"qty",
                           nil];
    NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"李念儿",@"name",
                           @"77", @"qty",
                           nil];
    NSDictionary *dict7 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"文彩依",@"name",
                           @"134", @"qty",
                           nil];
    NSDictionary *dict8 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"柳婵诗",@"name",
                           @"537", @"qty",
                           nil];
    NSDictionary *dict9 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"顾莫言",@"name",
                           @"213", @"qty",
                           nil];
    NSDictionary *dict10 = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"任水寒",@"name",
                            @"875", @"qty",
                            nil];
    [tablerray3 addObject:dict];
    [tablerray3 addObject:dict2];
    [tablerray3 addObject:dict3];
    [tablerray3 addObject:dict4];
    [tablerray3 addObject:dict5];
    [tablerray3 addObject:dict6];
    [tablerray3 addObject:dict7];
    [tablerray3 addObject:dict8];
    [tablerray3 addObject:dict9];
    [tablerray3 addObject:dict10];
    
    maxQty3=0;
    for(NSInteger i=0;i<[tablerray3 count];i++){
        NSDictionary *dict=[tablerray3 objectAtIndex:i];
        CGFloat qty=[[dict objectForKey:@"qty"]floatValue];
        if(qty>maxQty3){
            maxQty3=qty;
        }
    }
}

-(UIColor*)chooseColor:(NSInteger)index{
    
    index=index+1;
    UIColor *color=[[UIColor alloc]init];
    switch (index) {
        case 1:
            color=[UIColor colorWithRed:213.0/255.0 green:145.0/255.0 blue:48.0/255.0 alpha:255.0/255.0];
            break;
            
        case 2:
            color=[UIColor colorWithRed:60.0/255.0 green:123.0/255.0 blue:218.0/255.0 alpha:255.0/255.0];
            break;
            
        case 3:
            color=[UIColor colorWithRed:189.0/255.0 green:189.0/255.0 blue:189.0/255.0 alpha:255.0/255.0];
            break;
            
        case 4:
            color=[UIColor colorWithRed:195.0/255.0 green:0.0/255.0 blue:22.0/255.0 alpha:255.0/255.0];
            break;
            
        case 5:
            color=[UIColor colorWithRed:246.0/255.0 green:229.0/255.0 blue:24.0/255.0 alpha:255.0/255.0];
            break;
            
        case 6:
            color=[UIColor colorWithRed:111.0/255.0 green:206.0/255.0 blue:27.0/255.0 alpha:255.0/255.0];
            break;
            
        case 7:
            color=[UIColor colorWithRed:123.0/255.0 green:0.0/255.0 blue:253.0/255.0 alpha:255.0/255.0];
            break;
            
        case 8:
            color=[UIColor colorWithRed:60.0/255.0 green:123.0/255.0 blue:218.0/255.0 alpha:255.0/255.0];
            break;
            
        case 9:
            color=[UIColor colorWithRed:171.0/255.0 green:231.0/255.0 blue:116.0/255.0 alpha:255.0/255.0];
            break;
            
        case 10:
            color=[UIColor colorWithRed:70.0/255.0 green:224.0/255.0 blue:181.0/255.0 alpha:255.0/255.0];
            break;
            
        default:
              color=[UIColor colorWithRed:70.0/255.0 green:224.0/255.0 blue:181.0/255.0 alpha:255.0/255.0];
            break;
    }
    return  color;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    rowHeight=3*70.0+[tablerray count]*25+[tablerray2 count]*25+[tablerray3 count]*25;
    return rowHeight;
}


- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
    
    NSString *cellId = @"cellname" ;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :cellId];
    if (cell == nil ) {
        cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :cellId] ;
    }
    uv=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, rowHeight)];
    
    CGFloat height_=40;
    
    
    UILabel *lbcp=[[UILabel alloc]initWithFrame:CGRectMake(30, 10, width, 20)];
    lbcp.text=@"产品排行Top";
    lbcp.textColor=backcolor;
    lbcp.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv addSubview:lbcp];
    
    CGFloat maxWidth=150.0;
    
    
    for(NSInteger i=0;i<[tablerray count];i++){
        NSDictionary *dict=[tablerray objectAtIndex:i];
        UILabel *lbname=[[UILabel alloc]initWithFrame:CGRectMake(50, height_, 80, 20)];
        lbname.text=[dict objectForKey:@"name"];
        lbname.textColor=backcolor;
        lbname.font=[UIFont fontWithName:@"Arial" size:11.0];
        [uv addSubview:lbname];
        
        CGFloat qty=[[dict objectForKey:@"qty"]floatValue];
        
        CGFloat lineWidth=qty*maxWidth/maxQty;
        
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(130,  height_+2.5, lineWidth, 15)];
        line.backgroundColor=[self chooseColor:i];
        [uv addSubview:line];
        
        
        UILabel *lbqty=[[UILabel alloc]initWithFrame:CGRectMake(135+lineWidth, height_, width, 20)];
        lbqty.text=[[NSString alloc]initWithFormat:@"%.0f",qty];
        lbqty.textColor=backcolor;
        lbqty.font=[UIFont fontWithName:@"Courier" size:10.0];
        [uv addSubview:lbqty];
        
        height_=height_+25;
    }
    
    height_=height_+40;
    
    
    UILabel *lbkh=[[UILabel alloc]initWithFrame:CGRectMake(30, height_, width, 20)];
    lbkh.text=@"客户排行Top";
    lbkh.textColor=backcolor;
    lbkh.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv addSubview:lbkh];
    
    
    height_=height_+40;
    
    
    for(NSInteger i=0;i<[tablerray2 count];i++){
        NSDictionary *dict=[tablerray2 objectAtIndex:i];
        UILabel *lbname=[[UILabel alloc]initWithFrame:CGRectMake(50, height_, 80, 20)];
        lbname.text=[dict objectForKey:@"name"];
        lbname.textColor=backcolor;
        lbname.font=[UIFont fontWithName:@"Arial" size:11.0];
        [uv addSubview:lbname];
        
        CGFloat qty=[[dict objectForKey:@"qty"]floatValue];
        
        CGFloat lineWidth=qty*maxWidth/maxQty2;
        
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(130,  height_+2.5, lineWidth, 15)];
        line.backgroundColor=[self chooseColor:i];
        [uv addSubview:line];
        
        UILabel *lbqty=[[UILabel alloc]initWithFrame:CGRectMake(135+lineWidth, height_, width, 20)];
        lbqty.text=[[NSString alloc]initWithFormat:@"%.0f",qty];
        lbqty.textColor=backcolor;
        lbqty.font=[UIFont fontWithName:@"Courier" size:10.0];
        [uv addSubview:lbqty];
        
        height_=height_+25;
    }
    
    
    height_=height_+40;
    
    
    UILabel *lbyw=[[UILabel alloc]initWithFrame:CGRectMake(30, height_, width, 20)];
    lbyw.text=@"业务排行Top";
    lbyw.textColor=backcolor;
    lbyw.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv addSubview:lbyw];
    
    
    height_=height_+40;
    
    
    for(NSInteger i=0;i<[tablerray3 count];i++){
        NSDictionary *dict=[tablerray3 objectAtIndex:i];
        UILabel *lbname=[[UILabel alloc]initWithFrame:CGRectMake(50, height_, 80, 20)];
        lbname.text=[dict objectForKey:@"name"];
        lbname.textColor=backcolor;
        lbname.font=[UIFont fontWithName:@"Arial" size:11.0];
        [uv addSubview:lbname];
        
        CGFloat qty=[[dict objectForKey:@"qty"]floatValue];
        
        CGFloat lineWidth=qty*maxWidth/maxQty3;
        
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(130,  height_+2.5, lineWidth, 15)];
        line.backgroundColor=[self chooseColor:i];
        [uv addSubview:line];
        
        UILabel *lbqty=[[UILabel alloc]initWithFrame:CGRectMake(135+lineWidth, height_, width, 20)];
        lbqty.text=[[NSString alloc]initWithFormat:@"%.0f",qty];
        lbqty.textColor=backcolor;
        lbqty.font=[UIFont fontWithName:@"Courier" size:10.0];
        [uv addSubview:lbqty];
        
        height_=height_+25;
    }


    
    
    
    
    
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryView=uv;
    return cell;
    
}


- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated: YES completion: nil ];

}
@end
